<?php
/**
 * Borrow(借支管理)
 */
namespace Admin\Controller;

use Common\Controller\AdminbaseController;

class BorrowController extends AdminbaseController
{
    protected $borrow_model;
    //驻厂与企业绑定model
    protected  $user_bind_com_model;
    
    public function _initialize()
    {
        parent::_initialize();
        $this->borrow_model = D("Common/Borrow");
        $this->user_bind_com_model = D("Common/UserBindCom");
    }

    /*****
     * @info 员工借支申请列表
     * @author Mojianguo
     * @date 2017年1月28
     */
    public function index() {
    	$borrow_state = I('request.borrow_state','');
    	parent::dict_select(array(array(
    			'code' => 'BORROW_TYPE',
    			'name' => 'borrow_state',
    			'text' => '借支状态',
    			'value' => $borrow_state
    	)
    	));
    	$where = array();
    	if(get_is_zhuchang_admin()){  /***驻厂管理员添加公司ID条件**/
    		$tArray = array();
    		$comIDs = $this->user_bind_com_model
    		                         ->where(array("user_id"=>get_current_admin_id()))
    		                         ->field("com_id")->select();
    		foreach ($comIDs as $key => $comID){
    			$tArray[$key] = $comID['com_id'];
    		}
    	   $comIDs =  implode(",",$tArray);
    	   if(!empty($comIDs))
    	   $where['com.id'] =  array('in',"$comIDs");
    	}
    	if($borrow_state){
    		$where['borrow.state']=$borrow_state;
    	}
        $comname = I('request.com_name');
        empty($comname )?: $where['com.com_name'] =  array('like',"%$comname%");
        
        $count = $this->borrow_model
						        ->join('as borrow LEFT JOIN __COMS__ as com ON com.id = borrow.comID')
						        ->join('LEFT JOIN __PERSON__ as person ON person.id = borrow.personID')
                               ->where($where)->count();
        $page = $this->page($count, 20);
        $file_arr = array(
        		'borrow.id'
        		,'borrow.comID'
        		,'borrow.type',
        		'borrow.num',
        		'borrow.requestDate',
        		'borrow.confirmtime',
        		'borrow.pmoneytime',
        		'(SELECT name FROM cmf_users WHERE id= borrow.confirmID)as confirmName',
        		'(SELECT name FROM cmf_users WHERE id= borrow.pMoneyID)as pMoneyName',
        		'borrow.state',
        		'borrow.reason',
        		'com.com_name',
        		'person.name as personName'
        );
        $list = $this->borrow_model->field(implode(",",$file_arr))
        ->join('as borrow LEFT JOIN __COMS__ as com ON com.id = borrow.comID')
        ->join('LEFT JOIN __PERSON__ as person ON person.id = borrow.personID')
        ->where($where)
        ->order("borrow.requestDate DESC")
        ->limit($page->firstRow . ',' . $page->listRows)
        ->select();
       // $sql = $this->borrow_model->getLastSql();
        $this->assign('list', $list);
        $this->assign("page", $page->show('Admin'));
        $this->display();
    }

    function edit_state(){
        if (IS_AJAX) {
                $state = I('state');   /**目标状态**/
                $obj['state'] = $state;
                $current_admin_id = get_current_admin_id();
                $where = array("id"=>I('id'));
                if($state == 2 || $state == 3){ //2:审核通过 3:审核不通过
                	$obj['confirmID'] = $current_admin_id;
                	$obj['confirmTime'] =  time();
                }else if(4 == $state){  //已完成(已打款)
                	$obj['pMoneyID'] = $current_admin_id;
                	$obj['pMoneyTime'] =  time();
                }
                $result=$this->borrow_model->where($where)->save($obj);
                $sql = $this->borrow_model->getLastSql();
                if ($result !== false) {
                    $this->success("操作成功");
                }else{
                    $this->error("操作失败！");
                }
        }
    }

    /**
     * 删除企业
     */
    public function delete() {
        $id = I("get.id",0,'intval');
        $delete_state = C('COM_STATE')[2]['dict_value'];
        $this->borrow_model->com_state = $delete_state;
        $this->borrow_model->deleted_time = time();
        $rs= $this->borrow_model->where('id='.$id)->save(); // 根据条件更新记录
       if ($rs!==false) {
            $this->success("删除成功！");
        } else {
            $this->error("删除失败！");
        }
    }
}
